package com.wenwu.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wenwu.common.bean.RelicsArchaeologicalWithBLOBs;
import com.wenwu.common.bean.RelicsAttachment;
import com.wenwu.common.bean.RelicsWork;
import com.wenwu.common.exception.RelicsException;
import com.wenwu.common.page.PaginationVO;
import com.wenwu.dao.RelicsArchaeologicalMapper;
import com.wenwu.dao.RelicsAttachmentMapper;
import com.wenwu.dao.RelicsWorkMapper;
import com.wenwu.service.WorkArchaeologicalService;

@Service
public class WorkArchaeologicalServiceImpl implements WorkArchaeologicalService {

	@Autowired
	private RelicsArchaeologicalMapper relicsArchaeologicalMapper;
	@Autowired
	private RelicsWorkMapper relicsWorkMapper;
	
	@Autowired
	private RelicsAttachmentMapper attachmentMapper;
	public String addObj(RelicsArchaeologicalWithBLOBs relicsArchaeological,Integer workId) throws RelicsException {
		try{
			String result = null;
			int num = 0 ;
			List<RelicsAttachment> list = relicsArchaeological.getFileList();
			if (relicsArchaeological.getArchId() == null) {
				num += relicsArchaeologicalMapper.insertSelective(relicsArchaeological);
				if (relicsArchaeological.getArchId() > 0) {
					if (list != null && list.size() > 0) {
						for (RelicsAttachment attachment : list) {
							attachment.setAttachmentDesc("考古勘探附件");
							attachment.setTableOwner(relicsArchaeological.getArchId());// 外键
						}
						num += attachmentMapper.insertList(list);
					}
				}
				RelicsWork record = new RelicsWork();
				record.setArchId(relicsArchaeological.getArchId());
				record.setWorkId(workId);
				relicsWorkMapper.updateByPrimaryKeySelective(record);
				if (num > 0) {
					result = "考古勘探添加成功";
				} else {
					result = "考古勘探添加失败";
				}
			} else {
				num += attachmentMapper.deleteByKey(relicsArchaeological.getArchId(),relicsArchaeological.getOwnList());//先删除原有的附件，再添加
				if (list != null && list.size() > 0) {
					for (RelicsAttachment attachment : list) {
						attachment.setAttachmentDesc("考古勘探附件");
						attachment.setTableOwner(relicsArchaeological.getArchId());// 外键
					}
					num += attachmentMapper.insertList(list);
				}
				num += relicsArchaeologicalMapper.updateByPrimaryKeySelective(relicsArchaeological);
				if (num > 0) {
					result = "考古勘探修改成功";
				} else {
					result = "考古勘探修改失败";
				}
			}
			return result;
		}catch(Exception e){
			throw new RelicsException("系统错误，请稍后再试",e);
		}
	}

	public RelicsArchaeologicalWithBLOBs selectBySelftKey(Integer key) throws RelicsException {
		try{
			return relicsArchaeologicalMapper.selectByPrimaryKey(key);
		}catch(Exception e){
			throw new RelicsException("系统错误，请稍后再试",e);
		}
	}

	public String deleteBykeys(String keys) throws RelicsException {
		String result = "删除失败！";
		if (keys != null) {
			try {
				List<String> keyList = new ArrayList<String>();
				String[] objs = keys.split(",");
				for (String k : objs) {
					keyList.add(k);
				}
				relicsWorkMapper.clearBySomeId(keyList,"arch_id");
				result = "成功删除" + relicsArchaeologicalMapper.deleteInfoByKey(keyList) + "条数据";
			} catch (Exception e) {
				throw new RelicsException("系统错误，请稍后再试", e);
			}
		}
		return result;
	}

	public PaginationVO selectByCondition(RelicsArchaeologicalWithBLOBs relicsArchaeological,
			PaginationVO pageVo) throws RelicsException {
		try {
			List<RelicsArchaeologicalWithBLOBs> list = relicsArchaeologicalMapper.selectListByPage(relicsArchaeological, pageVo);
			int count = relicsArchaeologicalMapper.selectTotalRecord(relicsArchaeological);
			pageVo.setList(list);
			pageVo.setTotalRecord(count);
			return pageVo;
		} catch (Exception e) {
			throw new RelicsException("系统错误，请稍后再试", e);
		}
	}

	public RelicsArchaeologicalWithBLOBs selectByWorktKey(Integer workId)throws RelicsException {
		try{
			RelicsWork relicsWork = relicsWorkMapper.selectByPrimaryKey(workId);
			return relicsArchaeologicalMapper.selectByPrimaryKey(relicsWork.getArchId());
		}catch(Exception e){
			throw new RelicsException("系统错误，请稍后再试",e);
		}
	}

}
